package com.Dao;

import cn.hutool.core.date.DateUtil;
import com.Bean.SealOrder;
import com.Bean.User;
import com.Common.Constant;
import com.Common.PageInfo;
import com.Util.JdbcUtil;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

/**
 * @description:
 * @author: hkw14
 * @time: 2021/5/6 11:21
 */
public class SealOrderDao extends BaseDao{
    public PageInfo<SealOrder> selectPage(Map<String,Object> params, Integer page, Integer limit) {
        String sql = "select * from `salesOrder` where";
        //模糊查询
        if(params.containsKey("userName")) {
            sql = sql + "  userName like '%"+params.get("userName")+"%'   and";
        }
        if(params.containsKey("goodName")) {
            sql = sql + "  goodName like '%"+params.get("goodName")+"%'   and";
        }
        if(sql.endsWith("and")) {
            sql = sql.substring(0,sql.lastIndexOf("and"));
        }
        if(sql.endsWith("where")) {
            sql = sql.substring(0,sql.lastIndexOf("where"));
        }
        return this.selectPage(sql, SealOrder.class, page, limit);
    }

    public boolean batchDelete(String[] ids) {
        Connection conn = JdbcUtil.getConn();
        PreparedStatement prep = null;
        String sql = "DELETE FROM salesOrder WHERE ID=?;";

        try {
            //设置事务为手动提交
            conn.setAutoCommit(false);
            prep = conn.prepareStatement(sql);
            for (String id : ids) {
                prep.setInt(1, Integer.parseInt(id));
                prep.addBatch();
            }
            int[] count = prep.executeBatch();
            //手动提交事务
            conn.commit();
            return count.length > 0 ?true:false;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, prep);
        }
        return false;
    }

    public boolean add(Integer goodID, Integer userID, String goodName, Integer number, String userName, BigDecimal unitPrice) {
        BigDecimal sum = unitPrice.multiply(new BigDecimal(number));
        String createTime = DateUtil.format(new Date(), Constant.YYYY_MM_DD_HH_MM_SS);
        Connection conn = JdbcUtil.getConn();
//        System.out.println(conn);
//        System.out.println(Arrays.toString(ids));
        PreparedStatement prep1 = null;
        PreparedStatement prep2 = null;
        String sql1 = "update goods set number = number - ? where ID=?";
        String sql2 = "INSERT INTO  salesOrder (goodID, userID, creationTime, goodName, number, sum,userName,unitPrice) VALUES (?,?,?,?,?,?,?,?)";

        try {
            //设置事务为手动提交
            conn.setAutoCommit(false);
            prep1 = conn.prepareStatement(sql1);
            prep2 = conn.prepareStatement(sql2);
            prep1.setInt(1,number);
            prep1.setInt(2,goodID);
            prep2.setInt(1,goodID);
            prep2.setInt(2,userID);
            prep2.setString(3,createTime);
            prep2.setString(4,goodName);
            prep2.setInt(5,number);
            prep2.setBigDecimal(6,sum);
            prep2.setString(7,userName);
            prep2.setBigDecimal(8,unitPrice);
            int i = prep1.executeUpdate();
            int i1 = prep2.executeUpdate();
            //手动提交事务
            conn.commit();
            return (i > 0 & i1 > 0)?true:false;
        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, prep1);
            JdbcUtil.close(null, prep2);
        }
        return false;
    }
}
